package diandiansong.library.ui.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import diandiansong.library.R;
import diandiansong.library.utils.Logger;
import diandiansong.library.utils.OnLoadingListener;
import diandiansong.library.utils.UIHelper;


/**
 * Created by act on 2017/6/14.
 */

public class BaseAct extends AppCompatActivity implements OnLoadingListener {

    protected static Logger logger = Logger.getLogger();

    protected AppCompatActivity getActivity() {
        return this;
    }

    public Context getContext() {
        return this;
    }

    LoadingDialog loadingDialog;

    public void onLoading() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (loadingDialog != null) {
                    loadingDialog.close();
                }
                loadingDialog = new LoadingDialog(getActivity());
                loadingDialog.setLoadingText("加载中")
                        .setDrawColor(getResources().getColor(R.color.colorPrimary))
                        .show();
            }
        });

    }

    public void onLoaded() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (loadingDialog != null) {
                    loadingDialog.close();
                }
            }
        });

    }

    protected static int mScreenWidth = -1;
    protected static int mScreenHeight = -1;

    protected FrameLayout mContentView;
    protected View mTitleView;
    protected View mBackView;
    protected TextView mTitleTextView;
    protected TextView mRightView1;
    protected TextView mRightView2;
    protected ImageView mRightImageView1;
    protected ImageView mRightImageView2;

    protected Context mContext;

    protected Unbinder mUnbind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_base);

        mContext = this;

        if (mScreenWidth == -1) {
            Display mDisplay = getActivity().getWindowManager().getDefaultDisplay();
            mScreenWidth = mDisplay.getWidth();
            mScreenHeight = mDisplay.getHeight();
        }

        initView();
    }

    @Override
    protected void onDestroy() {
        if (mUnbind != null) {
            mUnbind.unbind();
        }
        super.onDestroy();
    }

    private void initView() {
        mContentView = (FrameLayout) findViewById(R.id.base_content);
        mTitleView = findViewById(R.id.title_view);
        mBackView = findViewById(R.id.title_back);
        mTitleTextView = (TextView) findViewById(R.id.title_text);
        mRightView1 = (TextView) findViewById(R.id.title_text_right_1);
        mRightView2 = (TextView) findViewById(R.id.title_text_right_2);
        mRightImageView1 = (ImageView) findViewById(R.id.title_iv_right_1);
        mRightImageView2 = (ImageView) findViewById(R.id.title_iv_right_2);
    }

    protected void addContentView(@LayoutRes int layoutResID) {
        View view = LayoutInflater.from(this).inflate(layoutResID, mContentView, true);
        mUnbind = ButterKnife.bind(this);
    }

    protected void addContentView(View view) {
        mContentView.addView(view);
        mUnbind = ButterKnife.bind(this);
    }

    protected void addFragment(String fragmentName) {
        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = Fragment.instantiate(this, fragmentName);

        fm.beginTransaction().add(mContentView.getId(), fragment).commit();
    }

    protected void addFragment(String fragmentName, Bundle b) {
        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = Fragment.instantiate(this, fragmentName, b);

        fm.beginTransaction().add(mContentView.getId(), fragment).commit();
    }

    protected void addFragment(Fragment fragment) {
        FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction().add(mContentView.getId(), fragment).commit();
    }

    protected void hideBack() {
        mBackView.setVisibility(View.INVISIBLE);
    }

    protected void hideTitle() {
        mTitleView.setVisibility(View.GONE);
    }


    public void initTitleOnly(String title) {
        mTitleTextView.setText(title);
        mTitleTextView.setVisibility(View.VISIBLE);
        mBackView.setVisibility(View.GONE);
        mRightImageView1.setVisibility(View.GONE);
        mRightImageView2.setVisibility(View.GONE);
        mRightView1.setVisibility(View.GONE);
        mRightView2.setVisibility(View.GONE);
        mTitleView.setVisibility(View.VISIBLE);
    }


    public void initTitle(String title) {
        mTitleTextView.setText(title);
        mTitleTextView.setVisibility(View.VISIBLE);
        mBackView.setVisibility(View.VISIBLE);
        mBackView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mTitleView.setVisibility(View.VISIBLE);
    }

    public void initRightImage(int drawableId, View.OnClickListener onClickListener) {
        mRightImageView1.setVisibility(View.VISIBLE);
        mRightImageView1.setOnClickListener(onClickListener);
        mRightImageView1.setImageResource(drawableId);
    }


    public void initRightImage(int drawableId1, View.OnClickListener onClickListener1, int drawableId2, View
            .OnClickListener onClickListener2) {
        mRightImageView1.setVisibility(View.VISIBLE);
        mRightImageView1.setOnClickListener(onClickListener1);
        mRightImageView1.setImageResource(drawableId1);

        mRightImageView2.setVisibility(View.VISIBLE);
        mRightImageView2.setOnClickListener(onClickListener2);
        mRightImageView2.setImageResource(drawableId2);
    }


    public void initRithtText(String right, View.OnClickListener rightTextOnClickListener) {
        mRightView1.setText(right);
        mRightView1.setVisibility(View.VISIBLE);
        mRightView1.setOnClickListener(rightTextOnClickListener);
    }

    public void initRithtText(String right1, View.OnClickListener rightTextOnClickListener1, String right2, View
            .OnClickListener rightTextOnClickListener2) {
        mRightView1.setText(right1);
        mRightView1.setVisibility(View.VISIBLE);
        mRightView1.setOnClickListener(rightTextOnClickListener1);

        mRightView2.setText(right2);
        mRightView2.setVisibility(View.VISIBLE);
        mRightView2.setOnClickListener(rightTextOnClickListener2);

    }


    /*====title==*/

    public static String getText(TextView textView) {
        return textView.getText().toString().trim();
    }

    public static void setText(TextView textView, String value) {
        if (textView != null) {
            textView.setText(value == null ? "" : value);
        }
    }

    public static void fastText(View view, int textViewId, String value) {
        TextView textView = view.findViewById(textViewId);
        if (textView != null) {
            textView.setText(value == null ? "" : value);
        }
    }

    public boolean checkInput(TextView... textViews) {
        for (TextView tv : textViews) {
            if (getText(tv).isEmpty()) {
                toast("请输入完整的信息");
                return false;
            }
        }
        return true;
    }

    public void toast(String msg) {
        UIHelper.toast(getActivity(), msg);
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    /*
     * ************Fragment相关方法************************************************
     *
     */
    protected Fragment currentFragment;

    /**
     * Fragment替换(当前destroy,新的create)
     */
    public void fragmentReplace(Fragment toFragment, boolean backStack) {
        fragmentReplace(mContentView.getId(), toFragment, backStack);
    }

    /**
     * Fragment替换(当前destroy,新的create)
     */
    public void fragmentReplace(@IdRes int id, Fragment toFragment, boolean backStack) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        String toClassName = toFragment.getClass().getSimpleName();
        if (manager.findFragmentByTag(toClassName) == null) {
            transaction.replace(id, toFragment, toClassName);
            if (backStack) {
                transaction.addToBackStack(toClassName);
            }
            transaction.commit();
        }
    }

    /**
     * Fragment替换(核心为隐藏当前的,显示现在的,用过的将不会destroy与create)
     */
    public void smartFragmentReplace(Fragment toFragment) {
        smartFragmentReplace(mContentView.getId(), toFragment);
    }

    /**
     * Fragment替换(核心为隐藏当前的,显示现在的,用过的将不会destroy与create)
     */
    public void smartFragmentReplace(@IdRes int id, Fragment toFragment) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        // 如有当前在使用的->隐藏当前的
        if (currentFragment != null) {
            transaction.hide(currentFragment);
        }
        String toClassName = toFragment.getClass().getSimpleName();
        // toFragment之前添加使用过->显示出来
        if (manager.findFragmentByTag(toClassName) != null) {
            transaction.show(toFragment);
        } else {// toFragment还没添加使用过->添加上去
            transaction.add(id, toFragment, toClassName);
        }
        try {
            transaction.commit();
            // toFragment更新为当前的
            currentFragment = toFragment;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void postDelayed(Runnable action, long delayMillis) {
        getWindow().getDecorView().postDelayed(action, delayMillis);
    }

}
